package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.node.JsonResult;
import cn.stylefeng.guns.modular.api.model.*;
import cn.stylefeng.guns.modular.hp.constant.ArticleEnum;
import cn.stylefeng.guns.modular.hp.constant.MenuJumpTypeEnum;
import cn.stylefeng.guns.modular.hp.model.*;
import cn.stylefeng.guns.modular.hp.service.*;
import com.alibaba.druid.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/homePage")
@Api(tags = "1.0", description = "市中医院门户首页", value = "门户首页API")
public class HomePageApiController {

	private final static Logger log = LoggerFactory.getLogger(HomePageApiController.class);
	
	@Autowired
    private HpProperties hpProperties;
	
	@Autowired
    private IMenuInfoService menuInfoService;
	
	@Autowired
    private IArticleInfoService articleInfoService;
	
	@Autowired
    private IExpertInfoService expertInfoService;
	
	@Autowired
    private IFriendlyLinkInfoService friendlyLinkInfoService;
	
	@Autowired
    private IPageContentInfoService pageContentInfoService;
	
	@Autowired
    private IHospitalDescInfoService hospitalDescInfoService;
	
	@SuppressWarnings("unchecked")
	@GetMapping("/getTopMenulist")
    @ApiOperation(value = "获取首页顶级栏目列表")
    public JsonResult<HomeMenuDTO> getTopMenulist() {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<MenuInfo> topMenuInfoList = this.menuInfoService.menuInfolist("top");
		
		List<HomeMenuDTO> menuList= new ArrayList<HomeMenuDTO>(); 

		//顶级栏目Map
		Map<String, HomeMenuDTO> topMenuMap = new HashMap<String, HomeMenuDTO>();

		if(topMenuInfoList != null && topMenuInfoList.size() > 0) {
			for (int i = 0; i < topMenuInfoList.size(); i++) {
				if(StringUtils.isEmpty(topMenuInfoList.get(i).getParentid()) || topMenuInfoList.get(i).getParentid().equals("0")){
					topMenuMap.put(topMenuInfoList.get(i).getId(), new HomeMenuDTO(topMenuInfoList.get(i).getId(), topMenuInfoList.get(i).getParentid(),
							topMenuInfoList.get(i).getName(), topMenuInfoList.get(i).getLinkUrl(), topMenuInfoList.get(i).getJumpType(), topMenuInfoList.get(i).getPri()));
				}
			}
			for (int i = 0; i < topMenuInfoList.size(); i++) {
				if(!StringUtils.isEmpty(topMenuInfoList.get(i).getParentid()) && !topMenuInfoList.get(i).getParentid().equals("0")){
					if(topMenuMap.containsKey(topMenuInfoList.get(i).getParentid())){
						HomeMenuDTO menuDTOData = new HomeMenuDTO(topMenuInfoList.get(i).getId(), topMenuInfoList.get(i).getParentid(),
								topMenuInfoList.get(i).getName(), topMenuInfoList.get(i).getLinkUrl(), topMenuInfoList.get(i).getJumpType(), topMenuInfoList.get(i).getPri());
						if(topMenuMap.get(topMenuInfoList.get(i).getParentid()).getChildrenList() == null){
							List<HomeMenuDTO> dataList = new ArrayList<>();
							dataList.add(menuDTOData);
							topMenuMap.get(topMenuInfoList.get(i).getParentid()).setChildrenList(dataList);
						}else{
							topMenuMap.get(topMenuInfoList.get(i).getParentid()).getChildrenList().add(menuDTOData);
						}
					}
				}
			}
		}

		//遍历Map
		for(Map.Entry<String, HomeMenuDTO> entry : topMenuMap.entrySet()){
			menuList.add(entry.getValue());
		}
		
		Collections.sort(menuList);

		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(menuList);
		
        return jr;
    }
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/getLeftMenulist")
    @ApiOperation(value = "获取首页右侧栏目列表")
    public JsonResult<LeftMenuDTO> getLeftMenulist() {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<MenuInfo> leftMenuInfoList = this.menuInfoService.menuInfolist("left");
		
		List<LeftMenuDTO> menuList= new ArrayList<LeftMenuDTO>(); 
		
		if(leftMenuInfoList != null && leftMenuInfoList.size() > 0) {
			for (int i = 0; i < leftMenuInfoList.size(); i++) {
				MenuInfo menuInfo = leftMenuInfoList.get(i);
				menuList.add(new LeftMenuDTO(menuInfo.getName(), menuInfo.getDesc(), menuInfo.getLinkUrl(), menuInfo.getJumpType() , menuInfo.getPri()));
			}
		}
		
		Collections.sort(menuList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(menuList);
		
        return jr;
    }
	
	@SuppressWarnings("unchecked")
	@GetMapping("/getDepartmentList")
    @ApiOperation(value = "获取首页科室列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "length", value = "获取科室列表的size", dataType = "Integer", paramType = "query")
    })
	public JsonResult<DepartmentDTO> getDepartmentList(@RequestParam Integer length) {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<ArticleInfo> departmentInfoList = this.articleInfoService.modularList(ArticleEnum.DEPARTMENT, null);
		
		List<DepartmentDTO> departmentDTOList = new ArrayList<DepartmentDTO>();
		
		if(departmentInfoList != null && departmentInfoList.size() > 0) {
			for (int i = 0; i < departmentInfoList.size(); i++) {
				ArticleInfo info = departmentInfoList.get(i);
				departmentDTOList.add(new DepartmentDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), MenuJumpTypeEnum.DETAILS));
			}
			Collections.sort(departmentDTOList);
		}
		
		if(departmentDTOList != null && departmentDTOList.size() > length) {
			departmentDTOList = departmentDTOList.subList(0, length);
		}
		
		PageVo page = new PageVo(1, length, departmentInfoList.size(), departmentDTOList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);
		
		return jr;
	}
	
	@SuppressWarnings("rawtypes")
	@GetMapping("/getHospitalDesc")
    @ApiOperation(value = "获取医院简介")
	public JsonResult getHospitalDesc() {
		JsonResult jr = new JsonResult();
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		
		HospitalDescInfo info =  this.hospitalDescInfoService.getHospitalDesc();
		
		if(info != null ) {
			dataMap.put("content", info.getDesc());
			dataMap.put("value", info.getLinkUrl());
			dataMap.put("pageType", MenuJumpTypeEnum.DETAILS);
			Map<String, Object> picMap = new HashMap<String, Object>();
			picMap.put("title", "医院简介");
			picMap.put("url", info.getPicUrl());
			dataMap.put("pictures", picMap);
			
			jr.setResultCode(0);
			jr.setResultMsg("请求成功！");
			jr.setResultData(dataMap);
		} else {
			jr.setResultCode(1);
			jr.setResultMsg("请求失败！");
			jr.setResultData(dataMap);
		}
		
		return jr;
	}
	
	@SuppressWarnings("unchecked")
	@GetMapping("/getNoticeList")
    @ApiOperation(value = "获取通知公告列表")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query")
	})
	public JsonResult<NoticeDTO> getNoticeList(@RequestParam Integer length) {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<ArticleInfo> infoList = this.articleInfoService.modularList(ArticleEnum.NOTICE, null);
		
		List<NoticeDTO> noticeDTOList = new ArrayList<NoticeDTO>();
		
		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				ArticleInfo info = infoList.get(i);
				noticeDTOList.add(new NoticeDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
			}
			//Collections.sort(noticeDTOList);
		}
		
		if(noticeDTOList != null && noticeDTOList.size() > length) {
			noticeDTOList = noticeDTOList.subList(0, length);
		}
		
		PageVo page = new PageVo(1, length, infoList.size(), noticeDTOList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);
		
		return jr;
	}
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/getNewsList")
    @ApiOperation(value = "获取新闻列表")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query")
	})
	public JsonResult<NewsDTO> getNewsList(@RequestParam Integer length) {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<ArticleInfo> infoList = this.articleInfoService.modularList(ArticleEnum.NEWS, null);
		
		List<NewsDTO> newsDTOList = new ArrayList<NewsDTO>();
		
		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				ArticleInfo info = infoList.get(i);
				newsDTOList.add(new NewsDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
			}
			//Collections.sort(noticeDTOList);
		}
		
		if(newsDTOList != null && newsDTOList.size() > length) {
			newsDTOList = newsDTOList.subList(0, length);
		}
		
		PageVo page = new PageVo(1, length, infoList.size(), newsDTOList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);
		
		return jr;
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@GetMapping("/getExpertDescList")
    @ApiOperation(value = "获取专家简介列表")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query")
	})
	public JsonResult<ExpertDTO> getExpertDescList(@RequestParam Integer length) {
		JsonResult jr = new JsonResult();
		
		List<ExpertInfo> infoList = this.expertInfoService.expertList();
		
		List<ExpertDTO> experDTOtList = new ArrayList<ExpertDTO>();
		
		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				ExpertInfo info = infoList.get(i);
				experDTOtList.add(new ExpertDTO(info.getName(), info.getName(), info.getRank(), info.getPicUrl(), info.getPageContentId(), info.getPri(), MenuJumpTypeEnum.DETAILS));
			}
			//Collections.sort(experDTOtList);
		}
		
		if(length > 0 && length < 15) {
			if(experDTOtList != null && experDTOtList.size() > length){
				experDTOtList = experDTOtList.subList(0, length);
			}
		}
		
		PageVo page = new PageVo(1, length, infoList.size(), experDTOtList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);
		
		return jr;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@GetMapping("/getModelInfoList")
    @ApiOperation(value = "获取各模块文章列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "modelName", value = "模块名称，可取值为：SRT-科研教学，HE-科普知识，KCM-对口帮扶，NG-护理园地，PBG-党建园地，MIP-医保政策，PA-药事管理，PR-政策法规", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query")
	})
	public JsonResult<ModelDTO> getModelInfoList(@RequestParam String modelName, @RequestParam Integer length) {
		JsonResult jr = new JsonResult();
		
		String model = null;
		
		modelName = modelName.toLowerCase();
		switch (modelName) {
		case ArticleEnum.SRT:
			model = ArticleEnum.SRT;
			break;
		case ArticleEnum.HE:
			model = ArticleEnum.HE;
			break;
		case ArticleEnum.KCM:
			model = ArticleEnum.KCM;	
			break;
		case ArticleEnum.NG:
			model = ArticleEnum.NG;
			break;
		case ArticleEnum.PBG:
			model = ArticleEnum.PBG;
			break;
		case ArticleEnum.MIP:
			model = ArticleEnum.MIP;
			break;
		case ArticleEnum.PA:
			model = ArticleEnum.PA;
			break;
		case ArticleEnum.PR:
			model = ArticleEnum.PR;
			break;
		default:
			model = null;
			break;
		}
		
		if(model != null) {
			List<ArticleInfo> infoList = this.articleInfoService.modularList(model, null);
			
			List<ModelDTO> modelList = new ArrayList<ModelDTO>();
			
			if(infoList != null && infoList.size() > 0) {
				for (int i = 0; i < infoList.size(); i++) {
					ArticleInfo info = infoList.get(i);
					modelList.add(new ModelDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
				}
				//Collections.sort(noticeDTOList);
			}
			
			if(modelList != null && modelList.size() > length) {
				modelList = modelList.subList(0, length);
			}
			
			PageVo page = new PageVo(1, length, infoList.size(), modelList);
			
			jr.setResultCode(0);
			jr.setResultMsg("请求成功！");
			jr.setResultData(page);
		}else {
			jr.setResultCode(1);
			jr.setResultMsg("请求失败，不存在的模块名！");
			jr.setResultData(null);
		}
		
		return jr;
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@GetMapping("/getFriendlyLinkList")
    @ApiOperation(value = "获取友情链接列表")
	public JsonResult<FriendlyLinkDTO> getFriendlyLinkList() {
		JsonResult jr = new JsonResult();
		
		List<FriendlyLinkInfo> infoList = this.friendlyLinkInfoService.friendlyLinkList();
		
		List<FriendlyLinkDTO> friendlyLinkDTOList = new ArrayList<FriendlyLinkDTO>();
		
		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				FriendlyLinkInfo info = infoList.get(i);
				friendlyLinkDTOList.add(new FriendlyLinkDTO(info.getName(), info.getPicUrl(), info.getLinkUrl(), info.getPri()));
			}
			//Collections.sort(experDTOtList);
		}
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(friendlyLinkDTOList);
		
		return jr;
	}
	
	@GetMapping("/getPage")
    @ResponseBody
	@ApiOperation(value = "根据ID获取页面内容")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "页面ID", dataType = "String", paramType = "query")
	})
	public String getPage(@RequestParam String id) {
		
		PageContentInfo page = this.pageContentInfoService.selectById(id);
		if(page != null ) {
			return page.getContent();
		}
		return "";
	}
}
